<template>
  <div>
    <!-- 功能按钮 -->
    <el-row :gutter="4" style="margin-bottom: 10px">
      <el-col :span="6">
        <el-button type="primary" @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>

    <!-- 树形表格 -->
    <el-table
      :data="data.tableData"
      row-key="permissionId"
      style="width: 100%"
      border
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      :header-cell-style="{ color: '#333', backgroundColor: '#eaf4ff' }"
    >
      <el-table-column prop="permissionId" label="ID" width="80" />
      <el-table-column prop="permissionName" label="菜单名称" min-width="180" />
      <el-table-column prop="url" label="组件路径" width="200" />
      <el-table-column prop="permissionResourcetype" label="类型" width="100" />
      <el-table-column prop="url" label="URL" min-width="200" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>


    </el-table>

    <!-- 新增弹窗 -->
    <el-dialog title="新增权限" v-model="dialogVisible" width="500px">
      <el-form :model="form" label-width="80px">
        <!-- <el-form-item label="ID">
          <el-input v-model="form.permissionId" placeholder="请输入ID" />
        </el-form-item> -->
        <el-form-item label="权限名称">
          <el-input v-model="form.permissionName" placeholder="请输入权限名称" />
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="form.permissionResourcetype" placeholder="请选择">
            <el-option label="menu" value="menu" />
            <!-- <el-option label="button" value="button" /> -->
          </el-select>
        </el-form-item>
        <el-form-item label="URL">
          <el-input v-model="form.url" placeholder="请输入URL，例：menu，menu/index" />
        </el-form-item>
        <el-form-item label="父级ID">
          <el-input v-model="form.parentId" placeholder="0 表示顶级" />
        </el-form-item>
        <el-form-item label="是否隐藏">
          <el-switch v-model="form.opFlag" active-text="是" inactive-text="否" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import request from "@/utils/request";
// 弹窗控制
const dialogVisible = ref(false);

// 表单
const form = reactive({
  permissionId: "",
  permissionName: "",
  permissionResourcetype: "",
  url: "",
  parentId: "0"
});

// 读取本地数据
const data = reactive({
  tableData: JSON.parse(localStorage.getItem("menuRoutes") || "[]")
});

// 点击新增
const handleAdd = () => {
  Object.assign(form, {
    permissionId: "",
    permissionName: "",
    permissionResourcetype: "",
    url: "",
    parentId: "0"
  });
  dialogVisible.value = true;
};

// 递归查找父节点并添加子节点
const addToTree = (tree, parentId, newNode) => {
  for (const node of tree) {
    if (node.permissionId === parentId) {
      if (!node.children) node.children = [];
      node.children.push(newNode);
      return true;
    }
    if (node.children && node.children.length) {
      if (addToTree(node.children, parentId, newNode)) return true;
    }
  }
  return false;
};

// 提交新增
const submitForm = () => {
  // if (!form.permissionName||!form.url||!form.parentId||form.permissionResourcetype) {
  //   return ElMessage.warning("不能为空");
  // }

  const newNode = {
    permissionName: form.permissionName,
    permissionResourcetype: form.permissionResourcetype,
    url: form.url,
    parentId: form.parentId,
    opFlag: form.opFlag,
    componentPath: form.url, // componentPath 与 url 相同
  };
  request.post('/permission/add', newNode).then((res) => {
    if(res.code === 200) {
      dialogVisible.value = false;
      ElMessage.success("新增成功");
      // 更新本地数据
      data.tableData.push({ ...newNode, permissionId: res.data.permissionId });
      localStorage.setItem("menuRoutes", JSON.stringify(data.tableData));
    } else {
      ElMessage.error(res.message || "新增失败");
    }
  });
};
</script>

<style scoped>
.content {
  margin-top: 10px;
}
</style>
